package com.example.controller;


import com.example.pojo.ResultBean;
import com.example.pojo.SysResource;
import com.example.service.ISysResourceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyj
 * @since 2023-02-16
 */
@RestController
@RequestMapping("/sys-resource")
public class SysResourceController {
    @Autowired
    private ISysResourceService sysResourceService;

    /**
     * 查询全部记录
     * @return
     */
    @GetMapping("queryAll")
    public ResultBean queryAll(){
        List<SysResource> list = sysResourceService.lambdaQuery().orderByAsc(SysResource::getSort).list();
        return ResultBean.success(list);
    }

    /**
     * 分页条件查询
     * @param name
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions({"menu:select"})
    public ResultBean list(String name){
        List<SysResource> list = sysResourceService.lambdaQuery()
                .like(!ObjectUtils.isEmpty(name), SysResource::getName, name)
                .list();
        return ResultBean.success(list);
    }

    /**
     * 根据userid查询
     * @param id
     * @return
     */
    @GetMapping("queryByUserId/{id}")
    public ResultBean queryByUserId(@PathVariable("id")Long id){
        return ResultBean.success(sysResourceService.queryByUserId(id));
    }

    /**
     * 新增或更新
     * @param sysResource
     * @return
     */
    @PostMapping("saveOrUpdate")
    @RequiresPermissions({"menu:select","company:detail","company:accounts"})
    public ResultBean saveOrUpdate(@RequestBody SysResource sysResource){
        System.out.println("新增或修改的数据：" + sysResource);
        boolean rs = sysResourceService.saveOrUpdate(sysResource);
        return rs ? ResultBean.success() : ResultBean.error();
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    @RequiresPermissions({"menu:select","company:detail","company:accounts"})
    public ResultBean delete(@PathVariable("id") Long id){
        System.out.println("要删除的id" + id);
        boolean rs = sysResourceService.removeById(id);
        return rs ? ResultBean.success() : ResultBean.error();
    }

    /**
     * 给角色分配权限
     * @param map
     * @return
     */
    @PostMapping("/assignPermissions")
    @RequiresPermissions({"menu:select","company:detail","company:accounts"})
    public ResultBean assignPermissions(@RequestBody Map<String,Object> map){
        System.out.println("分配权限");
        map.forEach((k,v) -> System.out.println(k + ":" + v));
        Object roleId = map.get("roleId");
        List permissions = (List) map.get("permissions");
        boolean rs = sysResourceService.assignPermissions((Serializable) roleId,permissions);
        return rs ? ResultBean.success() : ResultBean.error();
    }
}
